import { WebVisitConst } from "src/consts/web_visit_const";
import * as Api from 'src/api/net';
import { PopupName } from "src/consts/popup_const";

const state: ListMsgData = {} as any;
// StateIniter.init(state, 'demo');

state.curr_page = 1;
state.page_size = 15;
state.total_page = 1;
state.total_size = 1;
state.data_list = [];
state.type = "";
state.index = "";
// vuex

/** 更新数据的 */
const mutations = {
    [WebVisitConst.CommitWebVList](state: ListMsgData, data: ListMsgData): void {
        console.log("demo data update", data);
        state.curr_page = data.curr_page;
        state.page_size = data.page_size;
        state.total_page = data.total_page;
        state.total_size = data.total_size;
        let len = data.data_list.length;
        for (let i = 0; i < len; i++) {
            let tmp: DemoData = data.data_list[i];
            tmp.selected = false;
        }
        state.data_list = data.data_list;
    },
    [WebVisitConst.CommitType](state: ListMsgData, data: any): void {
        state.type = data.type
        state.index = data.index
        WLLib.panel(PopupName.WebV_Panel)
    }
}

//异步处理
const actions = {
    [WebVisitConst.DispatchWebVList](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/website/history/list.do", data, (res: any) => {
            if (isNoEmptyObject(res.data)) {
                WLCommit(WebVisitConst.CommitWebVList, res.data);
            }
        })
    },
    [WebVisitConst.DispatchWebVNowList](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/website/visit/list.do", data, (res: any) => {
            if (isNoEmptyObject(res.data)) {
                WLCommit(WebVisitConst.CommitWebVList, res.data);
            }
        })
    },
    [WebVisitConst.DispatchWebVAdd](context: VuexData, data?: any): void {
        Api.checkAjaxPost(Config.ajaxUrl + "/website/apply/add.do", data, (res: any) => {
            if (res.code === 0) {
                WLMessage("申请新增成功", "success")
                WLLib.panel(PopupName.WebV_Panel).close();
            } else {
                WLMessage(res.msg, "warning")
                var obj: any = {}
                obj.name = data.name
                obj["curr_page"] = 1
                obj["page_size"] = 15
                WLDispatch(WebVisitConst.DispatchWebVNowList, obj);
            }
        })
    },
    [WebVisitConst.DispatchWebVVisit](context: VuexData, data?: any): void {
        Api.AjaxPost(Config.ajaxUrl + "/webapply/apply/add.do", data, (res: any) => {
            WLMessage("申请访问成功", "success")
            WLLib.panel(PopupName.WebV_Panel).close();
        })
    },
}


export default {
    state,
    mutations,
    actions
}